"use strict";
cc._RF.push(module, '24450Zi/BdCFJFdvFFUwWuJ', 'user_order_detail_pop');
// scripts/user/user_order_detail_pop.js

// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
'use strict';
/*
 *订单详情界面
*/

cc.Class({
    extends: cc.Component,

    properties: {
        lablotteryName: cc.Label, //对应彩种名
        labIssue: cc.RichText, //对应期数
        labBetAmount: cc.Label, //订单金额
        labRewardAmount: cc.Label, //中奖金额    
        labBet: cc.RichText, //共多少单
        labOrderStatus: cc.Label, //订单状态
        labOrderNum: cc.Label, //订单号
        labOrderTime: cc.Label, //订单时间
        spWin: cc.Node, //中奖图片 
        fbTicketPop: cc.Prefab, //出票详情预制
        openBalls: cc.Prefab, //开奖球号预制
        orderDtailiItem: cc.Prefab, //订单内容预制
        ndNumberContent: cc.Node, //开奖号码 
        betNunsContent: cc.Node, //投注内容
        quickRechargePagePrefab: cc.Prefab, //支付界面预制
        labWait: cc.Label, //等待开奖中。。。文本
        _chaseNum: 0, //追号期数
        _data: null
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start: function start() {
        if (this._data != null) {
            this.lablotteryName.string = LotteryUtils.getLotteryTypeDecByLotteryId(this._data.LotteryCode);
            this.labIssue.string = "<color=#000000>" + "第 " + "</color>" + "<color=#3b85ff>" + this._data.IsuseNum + "</color>" + "<color=#000000>" + " 期" + "</color>";
            this.labBetAmount.string = LotteryUtils.moneytoServer(this._data.Amount) + '元宝';

            if (this._chaseNum > 0) {
                this.labOrderStatus.string = LotteryUtils.getChaseOrderStatusByStatus(this._data.OrderStatus);
            } else {
                if (LotteryUtils.getOrderStatusByStatus(this._data.OrderStatus) == '中奖') {
                    this.labOrderStatus.node.color = new cc.Color(230, 74, 64);
                } else {
                    this.labOrderStatus.node.color = new cc.Color(59, 133, 255);
                }
                this.labOrderStatus.string = LotteryUtils.getOrderStatusByStatus(this._data.OrderStatus);
            }
            this.labOrderNum.string = this._data.OrderNum;
            if (this._data.OrderTime != null) {
                this.labOrderTime.string = Utils.byTimeGetStr(this._data.OrderTime, "-", ":");
            }

            if (this._data.OrderStatus == 14) {
                //中奖
                this.spWin.active = true;
                this.labRewardAmount.node.color = new cc.Color(230, 74, 64);
                this.labRewardAmount.string = LotteryUtils.moneytoServer(this._data.WinMoney) + '元宝';
            }

            if (!this._data.OpenNumber) {
                //等待开奖中
                this.labWait.node.active = true;
            } else {
                this.labWait.node.active = false;
                var temp1 = this._data.OpenNumber; //开奖列表    
            }

            var temp2 = null;
            try {
                temp2 = eval('(' + this._data.Number + ')'); //投注内容
            } catch (error) {}
            var bopen = false;
            this.ndNumberContent.removeAllChildren();
            if (temp1 != null) {
                var tempArry = temp1.split(" ");
                bopen = true;
                if (this._data.LotteryCode == "202" || this._data.LotteryCode == "301") {
                    for (var i = 0; i < tempArry.length; i++) {
                        var ball = cc.instantiate(this.openBalls);
                        ball.getComponent('public_balls_order_item').init({
                            spNums: tempArry[i]
                            //nums:null,
                        });
                        this.ndNumberContent.addChild(ball);
                    }
                } else if (this._data.LotteryCode == "40010" || this._data.LotteryCode == "40020") {
                    for (var i = 0; i < tempArry.length; i++) {
                        var numball = cc.instantiate(this.openBalls);
                        numball.getComponent('public_balls_order_item').init({
                            spNums: tempArry[i]
                            //nums:null,
                        });
                        numball.getChildByName('sprite').active = false;
                        numball.getChildByName('labelNum').color = new cc.Color(116, 116, 116);
                        this.ndNumberContent.addChild(numball);
                    }
                }
            }
            var betNums = 0;
            if (temp2 != null) {
                var numbers = temp2.length;

                for (var i = 0; i < numbers; i++) {
                    betNums += parseInt(temp2[i].Data.length);
                    for (var j = 0; j < temp2[i].Data.length; j++) {
                        if (this._data.LotteryCode == "40010" || this._data.LotteryCode == "40020") {
                            var oldNums = "";
                            var playcode = temp2[i].PlayCode.toString();
                            oldNums = temp2[i].Data[j].Number;
                            var content = '<color=#747474>' + LotteryUtils.getPlayTypeDecByPlayIdNor(playcode, temp2[i].Data[j].isNorm) + '</color>' + "  ";
                            content += " " + '<color=#3b85ff>' + this.labBetAmount.string + '</color>';

                            var item = cc.instantiate(this.orderDtailiItem);
                            var nums = LotteryUtils.analyseRewardNumber(oldNums, temp1, playcode, this._data.LotteryCode, true);

                            item.getComponent('user_order_detail_item').init({
                                number: nums,
                                dec: content
                            });
                            this.betNunsContent.addChild(item);
                        } else {
                            var oldNums = "";
                            var playcode = temp2[i].PlayCode.toString();
                            oldNums = temp2[i].Data[j].Number;
                            var content = '<color=#747474>' + LotteryUtils.getPlayTypeDecByPlayIdNor(playcode, temp2[i].Data[j].isNorm) + '</color>' + "  ";
                            content += '<color=#3b85ff>' + temp2[i].Data[j].Bet + '</color>' + '<color=#747474>' + "注  " + '</color>';
                            content += '<color=#3b85ff>' + temp2[i].Data[j].Multiple + '</color>' + '<color=#747474>' + "倍  " + '</color>';
                            var amo = 0;
                            amo = parseInt(temp2[i].Data[j].Bet) * parseInt(temp2[i].Data[j].Multiple) * 2;
                            content += '<color=#3b85ff>' + amo.toString() + "元宝" + '</color>';
                            var item = cc.instantiate(this.orderDtailiItem);
                            var nums = LotteryUtils.analyseRewardNumber(oldNums, temp1, playcode, this._data.LotteryCode, true);

                            if (nums.indexOf("拖") > 0) {
                                nums = nums.substring(0, nums.indexOf("拖")) + '<br/>' + nums.substring(nums.indexOf("拖"), nums.length);
                            }

                            item.getComponent('user_order_detail_item').init({
                                number: nums,
                                dec: content
                            });
                            this.betNunsContent.addChild(item);
                        }
                    }
                }
            }
            this.labBet.string = '<color=#747474>' + "共 " + '</color>' + '<color=#3B85FF>' + betNums.toString() + '</color>' + '<color=#747474>' + " 单" + '</color>';
        }
    },


    init: function init(data) {
        //cc.log('订单详情',data);
        this._data = data;
    },

    //抄单购买
    onMore: function onMore() {
        this.getCurIssue();
    },

    //继续投注
    onKeepOn: function onKeepOn() {
        //window.Notification.emit("HallUI_toGotoRoom",this._data.LotteryCode);

    },

    getCurIssue: function getCurIssue() {
        var recv = function (ret) {
            ComponentsUtils.unblock();
            var obj = ret.Data;
            if (ret.Code === 0) {
                var isuseNum = obj.IsuseNum;
                var endTimeDate = Utils.getDateForStringDate(obj.EndTime.toString());
                var currentTimeStamp = parseInt(obj.ServerTime / 1000);
                var endTimeStamp = Date.parse(endTimeDate) / 1000;
                var leftTimeStamp = endTimeStamp - currentTimeStamp;
                if (leftTimeStamp <= 0) {
                    //当前期数已截止
                    ComponentsUtils.showTips("当前期数已截止");
                    return;
                } else {
                    var confirmCallback = function () {
                        var data = {
                            lotteryID: this._data.LotteryCode,
                            lotteryMoney: this._data.Amount,
                            isuseid: obj.IsuseNum,
                            endTime: obj.EndTime,
                            beginTime: obj.BeginTime,
                            baseData: this._data.Number,
                            otherData: "",
                            buyType: 0,
                            data: "",
                            gold: 0,
                            paymentType: 2
                        };
                        var balace = User.getBalance();
                        var toSerMoney = LotteryUtils.moneytoServer(this._data.Amount);
                        if (balace < toSerMoney) {
                            var quickPayCallback = function (ret) {
                                data.data = ret.data;
                                data.gold = ret.gold;
                                data.paymentType = ret.paymentType;
                                this.doBalancePay(data);
                            }.bind(this);
                            var quickRechargePage = cc.instantiate(this.quickRechargePagePrefab);
                            var canvas = cc.find("Canvas");
                            canvas.addChild(quickRechargePage);
                            quickRechargePage.getComponent("public_rechargePay_pop").init(dataRev.lotteryId, obj.IsuseNum, toSerMoney, dataRev.buyType, true, quickPayCallback);
                        } else {
                            this.doBalancePay(data);
                        }
                    }.bind(this);
                    var closeCallback = function () {}.bind(this);
                    var strDec = "";
                    strDec = "第" + obj.IsuseNum + "期支付订单金额" + LotteryUtils.moneytoServer(this._data.Amount) + "元宝。是否确认支付？";
                    ComponentsUtils.showAlertTips(2, strDec, closeCallback, "抄单购买", confirmCallback);
                }
            } else {
                ComponentsUtils.showTips(ret.Msg);
                return;
            }
        }.bind(this);

        var data = {
            Token: User.getLoginToken(),
            LotteryCode: this._data.LotteryCode
        };
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.GETRECURRENTISUSE, data, recv.bind(this), "POST");
        ComponentsUtils.block();
    },

    //账户余额支付
    doBalancePay: function doBalancePay(dataBase) {
        var betData = "";
        var istr = dataBase.baseData.substr(0, 1);
        if (istr != "[") {
            betData = "[" + dataBase.baseData + "]";
        } else {
            betData = dataBase.baseData;
        }

        if (dataBase.lotteryID == "40010" || dataBase.lotteryID == "40020") {
            //ComponentsUtils.showTips("当前彩种暂时无法抄单购买");
            var recv = function (ret) {
                ComponentsUtils.unblock();
                if (ret.Code === 0) {
                    ComponentsUtils.showTips("支付成功");
                    window.Notification.emit("moneyShow", null);
                    window.Notification.emit("updateUserUI", "");
                    this.onClose();
                } else {
                    ComponentsUtils.showTips(ret.Msg);
                }
            }.bind(this);

            var data = {
                Token: User.getLoginToken(),
                UserCode: User.getUserCode(),
                LotteryCode: dataBase.lotteryID,
                BuyType: dataBase.buyType, //0代购 1追号 2跟单
                Amount: dataBase.lotteryMoney,
                IsuseNum: dataBase.isuseid,
                RoomCode: this._data.RoomID,
                BetData: betData,
                BeginTime: dataBase.beginTime,
                EndTime: dataBase.endTime
            };
            CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.SPECIALBET, data, recv.bind(this), "POST");
            ComponentsUtils.block();
        } else {
            //ComponentsUtils.showTips("当前彩种暂时无法抄单购买");
            var recv = function (ret) {
                ComponentsUtils.unblock();
                if (ret.Code === 0) {
                    ComponentsUtils.showTips("支付成功");
                    window.Notification.emit("moneyShow", null);
                    window.Notification.emit("updateUserUI", "");
                    this.onClose();
                } else {
                    ComponentsUtils.showTips(ret.Msg);
                }
            }.bind(this);

            var data = {
                Token: User.getLoginToken(),
                IsuseNum: dataBase.isuseid,
                BeginTime: dataBase.beginTime,
                EndTime: dataBase.endTime,
                RoomCode: this._data.RoomID,
                BetData: betData,
                ChaseData: dataBase.otherData,
                UserCode: User.getUserCode(),
                LotteryCode: dataBase.lotteryID,
                BuyType: 0, //0代购 1追号 2跟单
                Amount: dataBase.lotteryMoney,
                Coupons: dataBase.data,
                PaymentType: dataBase.paymentType, //1.余额彩豆购彩券支付 2.余额支付 3.购彩券支付 4.彩豆支付 5.余额和购彩券支付 6.余额和彩豆支付 7.彩豆和购彩券支付
                Gold: dataBase.gold //彩豆支付
            };

            CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.REQUESTBET, data, recv.bind(this), "POST");
            ComponentsUtils.block();
        }
    },

    //复制到剪切板
    onCopy: function onCopy() {
        if (cc.sys.os == cc.sys.OS_ANDROID || cc.sys.os == cc.sys.OS_IOS) {
            ComponentsUtils.showTips("已复制");
            //PlatformUtils.copyToClipboard(this._data.OrderNum);
        } else {
            var save = function (e) {
                e.clipboardData.setData('text/plain', this._data.OrderNum);
                e.preventDefault();
            }.bind(this);

            document.addEventListener('copy', save);
            document.execCommand('copy');
            document.removeEventListener('copy', save);
            ComponentsUtils.showTips("已复制订单号到剪切栏");
        }
    },

    onClose: function onClose() {
        this.node.getComponent("Page").backAndRemove();
    }

    // update (dt) {},
});

cc._RF.pop();